<template>
  <div class="resource">
    <el-card shadow="hover">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="margin-left: 20px;"
          icon="el-icon-search"
          type="primary"
          size="small"
          @click="search"
        >
          搜索
        </el-button>
        <el-button
          icon="el-icon-refresh"
          size="small"
          @click="resetParams"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="params"
          size="small"
          label-width="140px"
        >
          <el-form-item label="证书订单编号：">
            <el-input
              v-model="params.orderSn"
              placeholder="输入订单编号"
              clearable
              @clear="search"
            />
          </el-form-item>
          <el-form-item label="证书名称:">
            <!-- <el-select
              v-model="params.certificateName"
              placeholder="请选择证书名称"
              @change="search"
            >
              <el-option
                v-for="item in certOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select> -->
            <el-input
              v-model="params.certificateName"
              placeholder="输入证书名称"
              clearable
              @clear="search"
            />
          </el-form-item>
          <el-form-item label="物流单号：">
            <el-input
              v-model="params.trackingNumber"
              placeholder="输入物流单号"
              clearable
              @clear="search"
            />
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select v-model="params.state" placeholder="请选择订单状态" clearable @change="search">
              <el-option
                v-for="item in stateOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <div class="container">
      <div class="handle-box">
        <!-- <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          class="mr10"
          >新增</el-button
        > -->
        <!-- <el-button
          type="danger"
          icon="el-icon-delete"
          class="handle-del mr10"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        > -->
      </div>

      <el-card shadow="hover">
        <div style="margin-top: 5px;margin-bottom: 15px;">
          <span>待邮寄纸质证书列表</span>
        </div>
        <el-table
          style="margin-top: 10px;"
          v-loading="loading"
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column
            type="selection"
            width="50"
            align="center"
          ></el-table-column> -->
          <el-table-column type="index" label="编号" align="center" />
          </el-table-column>
          <el-table-column
            prop="orderSn"
            label="证书订单编号"
            align="center"
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="certificateName"
            label="证书名称"
            align="center"
            min-width="150"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column label="订单状态" align="center" width="130">
            <template slot-scope="scope">
              <span>{{ status(scope.row.state) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="trackingNumber"
            label="物流单号"
            align="center"
            width="150"
          ></el-table-column>
          <el-table-column
            label="订单地址"
            min-width="150"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.address }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="支付方式"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.payment == 1">微信支付</span>
              <span v-if="scope.row.payment == 2">支付宝支付</span>
              <span v-if="scope.row.payment == 3">零钱支付</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="payMoney"
            label="支付金额"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column
            prop="totalMoney"
            label="订单金额"
            align="center"
            width="80"
          ></el-table-column>
          <!-- <el-table-column
            prop="discountsMoney"
            label="优惠金额"
            align="center"
            width="80"
          ></el-table-column> -->
          <el-table-column label="创建时间" align="center" min-width="170">
            <template slot-scope="scope">{{
              formatT(scope.row.createTime)
            }}</template>
          </el-table-column>
          <el-table-column
            label="操作"
            min-width="200"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-plus"
                v-if="scope.row.state == 2"
                @click="handleAddPost(scope.$index, scope.row)"
                >快递单号</el-button
              >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                v-if="scope.row.state == 3"
                @click="handleEditPost(scope.$index, scope.row)"
                >快递单号</el-button
              >
              <!-- <el-button
                type="warning"
                size="mini"
                icon="el-icon-search"
                v-if="scope.row.state == 3"
                @click="searchPost(scope.$index,scope.row)"
              >
                物流查询
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>

      <el-dialog :title="wuliuTitle" :visible.sync="wuliuVisible" width="40%">
        <el-form ref="wuliuForm" :model="wuliuForm" label-width="120px" :rules="wuliuRules">
          <el-form-item label="物流单号：" prop="trackingNumber">
            <el-input v-model="wuliuForm.trackingNumber"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancelPost">取 消</el-button>
          <el-button type="primary" @click="sumitPost">确 定</el-button>
        </span>
      </el-dialog>


      <el-dialog title="物流进度" :visible.sync="jinduVisible" width="50%">
        <el-timeline :reverse="reverse">
          <el-timeline-item
            v-for="(activity, index) in activities"
            :key="index"
            :timestamp="activity.timestamp">
            {{activity.content}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { queryorder_school,updateorder_school,delorder_school } from '@/api/order/orderschool'
import { certificate__List } from "@/api/certificate/certificateAds";
import { getToken } from "@/utils/auth";
export default {
  components: {},
  props: [],
  data() {
    return {
      cateList2: [],
      // 非多个禁用
      multiple: true,
      //删除
      ids: [],
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      stateOptions: [
        {
          id: 2,
          label: "已付款待发货"
        },
        {
          id: 3,
          label: "待收货"
        }
      ],
      examineList: [
        {
          id: 0,
          title: "取消订单"
        },
        {
          id: 1,
          title: "待付款"
        },
        {
          id: 2,
          title: "已付款待发货"
        },
        {
          id: 3,
          title: "待收货"
        },
        {
          id: 4,
          title: "完成订单"
        }
      ],
      loading: true,
      //数据
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
        state: 2,
        examineStatus:2,
        orderSn: undefined,
        trackingNumber: undefined
      },
      pageTotal: 0,

      certOptions: [],

      wuliuTitle: '',
      wuliuVisible: false,
      wuliuForm: {
        trackingNumber: ''
      },
      wuliuRules: {
        trackingNumber: [
          { required: true, message: "物流单号不能为空", trigger: "blur" }
        ]
      },
      jinduVisible: false,
      reverse: true,
      activities: [{
        content: '订单已送达',
        timestamp: '2020-12-25'
      }, {
        content: '订单在路上',
        timestamp: '2020-12-24'
      }, {
        content: '订单发出',
        timestamp: '2020-12-23'
      }]
    };
  },
  computed: {},
  created() {
    console.log(this.$route)
    if(this.$route.params.state){
      this.params.state = this.$route.params.state;
      this.getData00()
    }else{
      this.getData00();
    };
    this.getCertList();
  },
  mounted() {},
  watch: {},
  methods: {
    getData(){
      queryorder_school({
        pageNum: 1,
        pageSize: 10,
        state: 2
      }).then(res => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.pageTotal = res.total;
          this.loading = false;
        }
      }); 
    },
    //获取待支付证书列表
    getData00() {
      queryorder_school(this.params).then(res => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.pageTotal = res.total;
          this.loading = false;
        }
      });
    },
    resetParams() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        state: 2,
        examineStatus:2,
      };
      this.search();
    },
    getCertList() {
      certificate__List({
        pageNum: 1,
        pageSize: 10000
      }).then(res => {
        this.certOptions = res.rows;
        console.log(this.certOptions);
      });
    },

    // 键值配对
    status(val) {
      var str = this.examineList.find(function(item) {
        return item.id == val;
      });
      if (str) return str.title;
    },

    // 删除操作
    handleSelectionChange(val) {
      this.ids = val.map(item => item.id);
      this.single = val.length != 1;
      this.multiple = !val.length;
    },
    // 批量删除
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return company_delete(id);
        })
        .then(() => {
          this.getData00();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },

    // 触发搜索按钮
    search() {
      this.$set(this.params, "pageNum", 1);
      this.paramsFlag = 0;
      this.getData00();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.params, "pageNum", val);
      this.getData00();
    },
    searchPost(index,row){
      this.jinduVisible = true;
    },
    handleAddPost(index,row){
      // this.wuliuForm.trackingNumber = '';
      this.wuliuTitle = "添加物流单号";
      this.wuliuVisible = true;
      this.wuliuForm = row;
    },
    handleEditPost(index,row){
      this.wuliuForm = row;
      this.wuliuTitle = "修改物流单号";
      this.wuliuVisible = true;
    },
    sumitPost(){
      this.$refs["wuliuForm"].validate(valid => {
        if (valid) {
          console.log(this.wuliuForm,'物流单号')
          this.wuliuForm.state = 3;
          updateorder_school(this.wuliuForm).then(res => {
            if(res.code == 200){
              this.$message.success('保存成功');
              this.wuliuVisible = false;
              this.wuliuForm.trackingNumber = '';
              this.getData00();
            }else {
              this.$message.error('保存失败');
              this.wuliuVisible = false;
              this.getData00();
            }
          })
        }
      });
    },
    cancelPost(){
      this.wuliuVisible = false;
      this.wuliuForm.trackingNumber = '';
    }
  }
};
</script>

<style scoped>
.resource {
  padding: 20px 30px;
}
.handle-box {
  margin-bottom: 10px;
}

.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue2 {
  color: #409eff;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
.mr10 {
  margin-right: 10px;
}

.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
.study_pic {
  display: block;
  width: 60px;
  height: 60px;
  margin: auto;
  border-radius: 50%;
}
</style>
